package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.dao.CheckItemMapper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckGroupItem;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.swing.text.StyledEditorKit;
import java.util.List;

/**
 * 2024/7/4-15:51
 * 功能:
 */
@Service
@Slf4j
public class CheckItemServiceImpl implements CheckItemService {
    @Resource
    private CheckItemDao checkItemDao;
    @Resource
    private CheckItemMapper checkItemMapper;
    @Resource
    private CheckGroupService checkGroupService;

    @Override
    public List<CheckItem> findAll() {
        List<CheckItem> checkItems = checkItemDao.findAll();
        return checkItems;

    }

    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    /**
     * 修改item
     * @param checkItem
     */
    @Override
    public void edit(CheckItem checkItem) {
        checkItemMapper.edit(checkItem);
    }

    /**
     * 根据id查询item
     * @param id
     * @return
     */
    @Override
    public CheckItem findById(Integer id) {
        CheckItem checkItem = checkItemMapper.select(id);
        return checkItem;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        //分页插件
        PageHelper.startPage(currentPage, pageSize);
        List<CheckItem> items = checkItemMapper.findAll(queryPageBean.getQueryString());
        PageInfo<CheckItem> checkItemPageInfo = new PageInfo<>(items);

        return new PageResult(checkItemPageInfo.getTotal(), items);
    }

    @Override
    public Boolean delete(Integer id) {

        if (id == null) throw new RuntimeException("id不存在");
        CheckGroupItem exist = checkGroupService.isExist(id);
        if (exist != null){
            return false;

        }
        Boolean delete = checkItemMapper.delete(id);
        return delete;


    }
}
